﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataBaseTable;

public partial class Manage_WishZhuanTi_Mode : System.Web.UI.Page
{
    private WishZhuanTiID wztID = new WishZhuanTiID();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Background_tb.Attributes.Add("readonly", "true");
            ConBackground_tb.Attributes.Add("readonly", "true");
            FontColor_tb.Attributes.Add("readonly", "true");
            ZhuanTiData_tb.Attributes.Add("readonly", "true");
        }
        if (!string.IsNullOrEmpty(Request.QueryString["id"]))
        {
            if (!IsPostBack)
            {
                zhuantiID_hf.Value = Request.QueryString["id"];
                submit_btn.Text = "更新专题";
                this.initData();
            }
            submit_btn.Click += new EventHandler(UpdateZT_Click);
        }
        else
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(wztID.ID))
                {
                    this.picguid_hf.Value = wztID.ID;
                }
                else
                {
                    wztID.ID = Guid.NewGuid().ToString();
                    wztID.Update();
                    this.picguid_hf.Value = wztID.ID;
                }
                submit_btn.Text = "创建专题";
            }
            submit_btn.Click += new EventHandler(AddZT_Click);
        }
    }
    private void initData()
    {
        WishZhuanTiTable wztt = new WishZhuanTiTable(Convert.ToInt32( zhuantiID_hf.Value));
        if(wztt.ID==0)
            Response.Redirect("~/Manage/WishZhuanTi_List.aspx");
        this.picguid_hf.Value = wztt.ZTGuID;
        ZhuanTiName_tb.Text = wztt.ZhuanTiName;
        BunnerImg_hf.Value = wztt.BunnerImg;
        BunnerImg_img.ImageUrl = "~/WillZTimgaes/" + BunnerImg_hf.Value;
        BunnerImg_img.Visible = true;
        ZhuanTiData_tb.Text = wztt.ZhuanTiData;
        FontColor_tb.Text = wztt.FontColor;
        SponsorName_tb.Text = wztt.SponsorName;
        SponsorLink_tb.Text = wztt.SponsorLink;
        Background_tb.Text = GetCssProperty.Get(wztt.Background, "background-color").Replace("#", "");
        ConBackground_tb.Text = GetCssProperty.Get(wztt.ConBackground, "background-color").Replace("#", "");
        string Background_img_url = GetCssProperty.GetImg(wztt.Background);
        if (!string.IsNullOrEmpty(Background_img_url))
        {
            
            Background_rbl.SelectedIndex = 1;
            Background_pl.Visible = true;
            Background_img.Visible = true;
            Background_ddl.SelectedValue = GetCssProperty.Get(wztt.Background, "background-repeat");
            Background_hf.Value = Background_img_url;
            Background_img.ImageUrl = "~/WillZTimgaes/" + Background_img_url;
        }
        string ConBackground_img_url = GetCssProperty.GetImg(wztt.ConBackground);
        //ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('" + ConBackground_img_url + "');</script>");
        if (!string.IsNullOrEmpty(ConBackground_img_url))
        {
            ConBackground_rbl.SelectedIndex = 1;
            ConBackground_pl.Visible = true;
            ConBackground_img.Visible = true;
            ConBackground_ddl.SelectedValue = GetCssProperty.Get(wztt.ConBackground, "background-repeat");
            ConBackground_hf.Value = ConBackground_img_url;
            ConBackground_img.ImageUrl = "~/WillZTimgaes/" + ConBackground_img_url;
        }
    }
    protected void BunnerImg_up_btn_Click(object sender, EventArgs e)
    {
        //上传头部图片
        this.UpLoadPic(this.BunnerImg_up_fu, this.BunnerImg_img, this.BunnerImg_hf, "BunnerImg");
    }
    protected void Background_up_btn_Click(object sender, EventArgs e)
    {
        //上传专题网页背景图片
        this.UpLoadPic(this.Background_up_fu, this.Background_img, this.Background_hf, "Background");
    }
    protected void Background_SelectedIndexChanged(object sender, EventArgs e)
    {
        //背景样式
        if (Background_rbl.SelectedIndex == 0)
        {
            Background_pl.Visible = false;
        }
        else
        {
            Background_pl.Visible = true;
        }
    }
    protected void ConBackground_up_btn_Click(object sender, EventArgs e)
    {
        //上传内容背景图片
        this.UpLoadPic(this.ConBackground_up_fu, this.ConBackground_img, this.ConBackground_hf, "ConBackground");
    }
    protected void ConBackground_rbl_SelectedIndexChanged(object sender, EventArgs e)
    {
        //内容背景样式
        if (ConBackground_rbl.SelectedIndex == 0)
        {
            ConBackground_pl.Visible = false;
        }
        else
        {
            ConBackground_pl.Visible = true;
        }
    }
    protected void AddZT_Click(object sender, EventArgs e)
    {
        WishZhuanTiTable wztt = new WishZhuanTiTable();
        wztt.ZTGuID = this.picguid_hf.Value;
        wztt.BunnerImg = BunnerImg_hf.Value;
        wztt.ZhuanTiName = ZhuanTiName_tb.Text;
        wztt.ZhuanTiData = ZhuanTiData_tb.Text;
        wztt.FontColor = FontColor_tb.Text;
        wztt.SponsorName = SponsorName_tb.Text;
        wztt.SponsorLink = SponsorLink_tb.Text;
        wztt.Background += "background-color:#" + Background_tb.Text+";";
        if (Background_rbl.SelectedIndex == 1)
        {
            wztt.Background += "background-repeat:" + Background_ddl.SelectedValue+";";
            wztt.Background += "background-image:url('$path$" + Background_hf.Value + "');";
            //url("../Images/bunner.png")
        }
        wztt.ConBackground += "background-color:#" + ConBackground_tb.Text + ";";
        if (ConBackground_rbl.SelectedIndex == 1)
        {
            wztt.ConBackground += "background-repeat:" + ConBackground_ddl.SelectedValue + ";";
            wztt.ConBackground += "background-image:url('$path$" + ConBackground_hf.Value + "');";
        }
        try
        {
            wztt.Insert();
            wztID.ID = "";
            wztID.Update();
            ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('创建专题成功');window.location.href='WishZhuanTi_List.aspx'</script>");
        }
        catch
        {
            ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('创建专题失败');</script>");
        }
    }
    protected void UpdateZT_Click(object sender, EventArgs e)
    {
        WishZhuanTiTable wztt = new WishZhuanTiTable(Convert.ToInt32(zhuantiID_hf.Value));
        if (wztt.ID == 0)
            Response.Redirect("~/Manage/WishZhuanTi_List.aspx");
        wztt.BunnerImg = BunnerImg_hf.Value;
        wztt.ZhuanTiName = ZhuanTiName_tb.Text;
        wztt.ZhuanTiData = ZhuanTiData_tb.Text;
        wztt.FontColor = FontColor_tb.Text;
        wztt.SponsorName = SponsorName_tb.Text;
        wztt.SponsorLink = SponsorLink_tb.Text;
        wztt.Background = "";
        wztt.Background += "background-color:#" + Background_tb.Text + ";";
        if (Background_rbl.SelectedIndex == 1)
        {
            wztt.Background += "background-repeat:" + Background_ddl.SelectedValue + ";";
            wztt.Background += "background-image:url('$path$" + Background_hf.Value + "');";
        }
        wztt.ConBackground = "";
        wztt.ConBackground += "background-color:#" + ConBackground_tb.Text + ";";
        if (ConBackground_rbl.SelectedIndex == 1)
        {
            wztt.ConBackground += "background-repeat:" + ConBackground_ddl.SelectedValue + ";";
            wztt.ConBackground += "background-image:url('$path$" + ConBackground_hf.Value + "');";
        }
        try
        {
            wztt.Update();
            ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('更新专题成功');</script>");
        }
        catch
        {
            ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('更新专题失败');</script>");
        }

    }
    private void UpLoadPic(FileUpload fu, Image pic, HiddenField picName, string ext)
    {
        string filepath = fu.PostedFile.FileName;
        string fileextname = filepath.Substring(filepath.LastIndexOf(".") + 1);//扩展名
        if (!(fileextname.ToLower() == "jpg" || fileextname.ToLower() == "jpeg" || fileextname.ToLower() == "gif" || fileextname.ToLower() == "png" || fileextname.ToLower() == "bmp"))
        {
            ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('选择的文件不是图片格式');</script>");
            return;
        }
        string mpath = Server.MapPath("~/WillZTimgaes/");//文件夹路径
        picName.Value = this.picguid_hf.Value + "_" + ext + "." + fileextname;
        string mfilename = this.picguid_hf.Value + "_" + ext;
        try
        {
            fu.PostedFile.SaveAs(mpath + mfilename  + "." + fileextname);
            pic.ImageUrl = "~/WillZTimgaes/" +picName.Value;
            pic.Visible = true;
            ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('添加图片成功');</script>");
            //DataBd();
        }
        catch
        {
            ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('添加图片失败');</script>");
        }
    }
}